package com.example.demo.io;

/**
 * volatile 保证变量线程间可见性
 */
public class VolatileTest {


    public static void main(String[] args) throws InterruptedException {
        Cs cs = new Cs();
        Thread thread = new Thread(cs::cau);
        thread.start();

        // main线程 停止2秒钟
        Thread.sleep(2000L);
        cs.running = false;
    }
}

class Cs {
    volatile boolean running = true;// 定义一个变量标识可执行状态


    public void cau(){
        System.out.println("开始计算....");
        while(running){

        }
        System.out.println("停止计算");
    }
}
